#!/bin/bash

# Slackware build script for mktoc

# Written by Luis Henrique <email removed>
# Now maintained by B. Watson <urchlay@slackware.uk>

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20211126 bkw: add README note about floating point samples.
# 20180111 bkw:
# - take over maintenance
# - BUILD=2
# - i486 => i586
# - don't include the install instructions in the package
# - get rid of some useless boilerplate
# - fix upstream typo/illiteracy: you LOSE information, you don't LOOSE it!
#   (sorry, this is a pet peeve, I can't not fix it)
# - add man page, basically upstream's README

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=mktoc
VERSION=${VERSION:-1.3}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# 20180111 bkw: No CFLAGS here, this is pure Python code. Would be noarch,
# except for the lib vs lib64 thing.

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find . -type d -print0 | xargs -0 chmod 755
find . -type f -print0 | xargs -0 chmod 644

# You know, I never saw the "loose" for "lose" typo until the internet
# age. Does that mean computers make people illiterate?
patch -p1 < $CWD/lose_not_loose.diff

python setup.py install --root=$PKG

mkdir -p $PKG/usr/man/man1
gzip -9c $CWD/$PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a CHANGELOG.rst LICENSE PKG-INFO README $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
